package com.fjec.gateway.sentinel;

import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.BlockResponse;
import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {

    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public BlockResponse fallbackResponse(String route, Throwable throwable) {
        // 记录日志
        if (throwable != null) {
            log.error(throwable.getMessage(), throwable);
        }
        if (throwable instanceof BlockException) {
            MyBlockResponse response = new MyBlockResponse(false, 429, "访问过于频繁", route);
            return response;
        } else {
            MyBlockResponse response = new MyBlockResponse(false, 500, "System Error", route);
            return response;
        }
    }
}
